#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

union semun{
	int val;
};
int main(){
	key_t key=ftok("./",01);
	if(key==-1){
		perror("ftok:");
		exit(1);
	}
	int semid=semget(key,2,0666|IPC_CREAT);
	if(semid==-1){
		perror("semget:");
		exit(1);
	}
	union semun sem;
	sem.val=0;
	int ret=semctl(semid,0,SETVAL,sem.val);
	if(ret==-1){
		perror("semctl:");
		exit(1);
	}
	ret=semctl(semid,0,GETVAL);
	printf("ret=%d\n",ret);
	sem.val=1;
	ret=semctl(semid,1,SETVAL,sem.val);
	ret=semctl(semid,1,GETVAL);
	printf("ret=%d\n",ret);
	if(ret==-1){
		perror("semctl:");
		exit(1);
	}
	system("ipcs -s");
	semctl(semid,0,IPC_RMID);
	system("ipcs -s");
	return 0;
}
